<?php

namespace System\Core;

if (!defined('BASEPATH')) die('Access Denied.');

/**
 * µMVC Framework (c) 2011.
 * 
 * PHP5 MVC Framework software for PHP 5.2.17 or later
 * 
 * @package         µMVC
 * @filename        system/core/log.class.php
 * @copyright       Lanz (c) 2011.
 * 
 */

/**
 * Log class
 * Handles log messages written to a file.
 * 
 * @package         µMVC
 * @subpackage      Core
 * @category        System
 */
class Log extends BaseSingleton
{
    const ERROR = 1;
    const DEBUG = 2;
    const INFO = 3;
    const ALL = 4;

    /**
     * Path to the log file.
     * @var string
     */
    protected $_logPath;

    /**
     * Holds the type of log entries that will be written to file.
     * @var int
     */
    protected $_threshold = 1;

    /**
     * Displays entries on screen if TRUE
     * @var boolean
     */
    protected $_verbose = TRUE;
    
    /**
     * The date format for each entry in the log file.
     * @var string
     */
    protected $_dateFmt = 'Y-m-d H:i:s';

    /**
     * Is the logging enabled?
     * @var boolean
     */
    protected $_enabled = TRUE;

    /**
     * Holds the different kind of log entries that can be made.
     * @var array
     */
    protected $_levels = array('1' => 'ERROR', '2' => 'DEBUG', '3' => 'INFO', '4' => 'ALL');

    function __construct()
    {
        $config = Config::instance()->getConfig();

        $this->_logPath = ($config['log_path'] != '') ? $config['log_path'] : BASEPATH . 'logs' . DS;
        if (!is_dir($this->_logPath) OR !is_writable($this->_logPath))
        {
            $this->_enabled = FALSE;
        }

        if (is_numeric($config['log_threshold']))
        {
            $this->_threshold = $config['log_threshold'];
        }

        if (is_bool($config['log_verbose']))
        {
            $this->_verbose = $config['log_verbose'];
        }
        
        if ($config['log_date_format'] != '')
        {
            $this->_date_fmt = $config['log_date_format'];
        }
    }

    /**
     *
     * @param string $level
     * @param string $msg
     * @return type 
     */
    public function write($level, $msg)
    {
        if ($this->_enabled === FALSE)
        {
            return FALSE;
        }

        if (!is_int($level) OR $level < 0 OR $level > 4 OR ($level > $this->_threshold))
        {
            return FALSE;
        }
        
        $message = $this->_levels[$level] . ' ' . (($level == Log::INFO) ? ' -' : '-') . ' ' . date($this->_dateFmt) . ' --> ' . $msg . "\n";
        
        if ($this->_verbose)
        {
            echo $message . '<br>';
        }

        $filepath = $this->_logPath . 'log-' . date('Y-m-d') . '.php';

        if (!file_exists($filepath))
        {
            $message .= "<" . "?php  if (!defined('BASEPATH')) exit('Access Denied.'); ?" . ">\n\n" . $message;
        }

        if (!$fp = @fopen($filepath, 'ab'))
        {
            return FALSE;
        }

        flock($fp, LOCK_EX);
        fwrite($fp, $message);
        flock($fp, LOCK_UN);
        fclose($fp);

        @chmod($filepath, 0666);

        return TRUE;
    }

}

?>
